/***************************************************************************
 *   Copyright (C) 2007 by Warren Kenny   *
 *   warren.kenny@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include <otaConfigReader.h>


otaConfigReader::otaConfigReader()
{
#ifndef WIN32
	m_configFile = "/etc/ota.conf";
#else
	m_configFile = "ota.conf";
#endif
}

otaConfigReader::otaConfigReader(std::string fileName)
{
	m_configFile = fileName;
}

bool otaConfigReader::readFile(std::string fileName)
{
	m_configFile = fileName;
	return readFile();
}

bool otaConfigReader::readFile()
{
	m_configuration.clear();
	std::string line, field, value;
	size_t marker;
	std::ifstream config(m_configFile.c_str());
  	
	if (config.is_open())
	{
		while (!config.eof() )
		{
			getline (config,line);
			if(line.compare(0, 1, "#") != 0)
			{
				marker = line.find("=");
				if(marker != std::string::npos)
				{
					field = line.substr(0, marker);
					value = line.substr(marker+1);
      					
					field = trim(field);
					value = trim(value);
      					
					if(field.length() > 5 && value.length() > 0)
					{
						m_configuration[ field ] = value;
					}
				}
			}
		}
		config.close();
		return true;
	}
	else
	{
		return false;
	}	 
}

std::string otaConfigReader::slice(std::string target, std::string separator, unsigned int &offset)
{
	size_t marker;
	std::string offsetString = target.substr( offset );
	marker = offsetString.find( separator );
	if( marker != std::string::npos )
	{
		offset = marker+1;
		return trim(offsetString.substr( 0, marker ));
	}
	else if( offset < target.size() )
	{	
		offset = target.size();
		return trim(offsetString);
	}
	else
	{
		return "EOS";
	}
}

std::string otaConfigReader::trim(std::string target)
{
	std::string whitespaces(" \t\f\v\n\r");
	size_t marker;
	
	marker = target.find_last_not_of(whitespaces);
	if(marker != std::string::npos)
		target.erase(marker+1);
      						
	marker = target.find_first_not_of(whitespaces);
	if(marker != 0)
		target.erase(0, marker);
		
	return target;
}

bool otaConfigReader::getConfiguration(std::string field, std::string &fieldValue)
{
	std::map< std::string, std::string >::iterator it = m_configuration.find( field );
	if( it != m_configuration.end() )
	{
		fieldValue = it->second;
		return true;
	}
	else
	{
		std::cout << "Unable to locate configuration field '" + field + "' in the specified configuration file" << std::endl; 
		return false;
	}
}

otaConfigReader::~otaConfigReader()
{
	
}
